package com.ydc.ln.commons.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 
 * @author ydc
 * 
 */
public class DateUtil {
	/**
	 * 计算日期的天数差
	 * 
	 * @param now
	 * @param returnDate
	 * @return
	 */
	private int daysBetween(Date now, Date returnDate) {
		Calendar cNow = Calendar.getInstance();
		Calendar cReturnDate = Calendar.getInstance();
		cNow.setTime(now);
		cReturnDate.setTime(returnDate);
		setTimeToMidnight(cNow);
		setTimeToMidnight(cReturnDate);
		long todayMs = cNow.getTimeInMillis();
		long returnMs = cReturnDate.getTimeInMillis();
		long intervalMs = todayMs - returnMs;
		return millisecondsToDays(intervalMs);
	}

	private int millisecondsToDays(long intervalMs) {
		return (int) (intervalMs / (1000 * 86400));
	}

	private void setTimeToMidnight(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
	}

	/**
	 * 把符合日期格式的字符串转换为日期类型
	 * 
	 * <pre>
	 * eg.Date d = DateUtil.stringToDate(&quot;3/3/2011&quot;, &quot;dd/MM/yyyy&quot;, new ParsePosition(0));
	 * </pre>
	 * 
	 * @param dateStr
	 * @param format
	 * @param pos
	 * @return
	 */
	public static java.util.Date stringToDate(String dateStr, String format, ParsePosition pos) {
		Date d = null;
		SimpleDateFormat formater = new SimpleDateFormat(format);
		try {
			// formater.setLenient(false);
			d = formater.parse(dateStr, pos);
		} catch (Exception e) {
			d = null;
		}
		return d;
	}

	/**
	 * 把日期转换为字符串
	 * 
	 * <pre>
	 * eg.Date d = DateUtil.stringToDate(&quot;3/3/2011&quot;, &quot;dd/MM/yyyy&quot;, new ParsePosition(0));
	 * String dateStr = DateUtil.dateToString(d, &quot;dd/MM/yyyy&quot;);
	 * </pre>
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToString(java.util.Date date, String format) {
		String result = "";
		SimpleDateFormat formater = new SimpleDateFormat(format);
		try {
			result = formater.format(date);
		} catch (Exception e) {
			result = "";
		}
		return result;
	}

	/**
	 * 字符串日期比较
	 * 
	 * @param dateStr1
	 * @param dateStr2
	 * @param format
	 * @return date1=date2 0; date1<date2 小于0; date1>date2 大于0
	 */
	public static int dateStrCompare(String dateStr1, String dateStr2, String format) {
		DateFormat df = new SimpleDateFormat(format);
		int result = 0;
		try {
			result = df.parse(dateStr1).compareTo(df.parse(dateStr2));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	 * @param date
	 * @return
	 */
	public static long getDateMills(java.util.Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getTimeInMillis();
	}

	public static java.sql.Timestamp date2Timestamp(java.util.Date date) {
		return new Timestamp(DateUtil.getDateMills(date));
	}

	/**
	 * 计算某年是什么年，某天是第几周，星期几
	 * 
	 */

	public static void test() {
		WeekDayCount wdc = new WeekDayCount(2011, 395);
	}

	/**
	 * 根据日期计算属于第几周，星期一为一周开始 copy start
	 */
	/**
	 * 根据日期计算属于第几周
	 * 
	 * @param date
	 *            格式 yyyy-MM-dd
	 * @throws ParseException
	 */
	public static int getWeekOfYear(String date) {

		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setFirstDayOfWeek(Calendar.MONDAY); // 设置每周的第一天为星期一
			// cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 每周从周一开始
			cal.setMinimalDaysInFirstWeek(1); // 设置每周最少为1天
			cal.setTime(df.parse(date));
			return cal.get(Calendar.WEEK_OF_YEAR);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	// copy end
	/**
	 * 本周第一天与最后一天的日期
	 * 
	 * 西方的一周第一天是星期天，最后一天是星期六。 中国习惯是星期一第一天，星期天最后一天。
	 * 
	 * copy start
	 * 
	 */
	public static java.sql.Date toSqlDate(Calendar c) {
		return new java.sql.Date(c.getTimeInMillis());
	}

	public static Calendar getFirstDayOfWeek() {
		Calendar monday = Calendar.getInstance();
		return getADayOfWeek(monday, Calendar.MONDAY);
	}

	public static Calendar getFirstDayOfWeek(Calendar day) {
		Calendar monday = (Calendar) day.clone();
		return getADayOfWeek(monday, Calendar.MONDAY);
	}

	public static Calendar getLastDayOfWeek() {
		Calendar sunday = Calendar.getInstance();
		return getADayOfWeek(sunday, Calendar.SUNDAY);
	}

	public static Calendar getLastDayOfWeek(Calendar day) {
		Calendar sunday = (Calendar) day.clone();
		return getADayOfWeek(sunday, Calendar.SUNDAY);
	}

	private static Calendar getADayOfWeek(Calendar day, int dayOfWeek) {
		int week = day.get(Calendar.DAY_OF_WEEK);
		if (week == dayOfWeek)
			return day;
		int diffDay = dayOfWeek - week;
		if (week == Calendar.SUNDAY) {
			diffDay -= 7;
		} else if (dayOfWeek == Calendar.SUNDAY) {
			diffDay += 7;
		}
		day.add(Calendar.DATE, diffDay);
		return day;
	}

	// copy end

	public static void main(String[] args) {
		Date d = DateUtil.stringToDate("3/3/2011", "dd/MM/yyyy", new ParsePosition(0));
		String dateStr = DateUtil.dateToString(d, "dd/MM/yyyy");
		System.out.println(dateStr);
		dateStr = DateUtil.dateToString(d, "yyyy/MM/dd hh:mm:ss");
		System.out.println(dateStr);

		System.out.println(dateStrCompare("6/3/2011", "3/3/2011", "dd/MM/yyyy"));

		System.out.println(DateUtil.dateToString(new Date(), "yyyy-MM-dd hh:mm:ss"));
		System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));

		System.out.println(DateUtil.getDateMills(new Date()));
	}
}

class WeekDayCount {
	int day;
	int weekday, weekCount, lastweekday, count;
	int year;

	public WeekDayCount(int year, int day) {
		this.year = year;
		this.day = day;
		this.yearJudge();
		this.weekDayCount();

	}

	public void yearJudge() {
		if (this.year % 4 == 0 && this.year % 100 != 0 || this.year % 400 == 0) {
			this.year = 366;
			System.out.println(this.year + "年是闰年，共有" + this.year + "天");
		} else {
			this.year = 365;
			System.out.println(this.year + "年是平年，共有" + this.year + "天");
		}
	}

	public void weekDayCount() {

		if (this.day > this.year) {
			System.out.println("输入错误，这年只有" + this.year + "天");
		} else {
			if (this.day % 7 == 0) {
				weekCount = this.day / 7;
				weekday = 7;
			} else {
				weekCount = this.day / 7 + 1;
				weekday = this.day % 7;
			}
			System.out.println("第" + this.day + "天是第" + weekCount + "周，星期" + weekday);
		}
	}
}
